limsOutsourcers.detailPage = {

    getBtnToolbar: function () {
        var _this = this
        return {
            type: 'btnToolbar',
            items: [{
                type: 'button',
                text: 'GIKAM.BUTTON.BACK',
                icon: 'back',
                onClick: function () {
                    workspace.window.goBack()
                }
            }, /*{
                type: 'button',
                text: 'GIKAM.BUTTON.VIEWAUDIT',
                icon: 'view-audit',
                onClick: function () {
                    Gikam.viewAudit('T_LIMS_OUTSOURCERS', _this.param.id)
                }
            }*/]
        }
    },

    getOutsourcerProductMainGrid: function () {
        let _this = this
        return {
            type: 'grid',
            id: 'lims-outsourcers-product-main-edit-list-grid',
            url: limsOutsourcers.baseProductMainUrl + '/queries/raw',
            requestData: {
                outsourcerId_EQ: _this.param.id
            },
            service: 'limsOutsourcersServiceImpl',
            dbTable: 'T_LIMS_OUTSOURCERS_PRODUCT_MAIN',
            columns: limsOutsourcers.getProductMainEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                }
            },
            onRowActive: function (index, row) {
            },
            page: true,
            deleteFormatter: function (row) {
                return Gikam.propI18N('T_LIMS_OUTSOURCERS_PRODUCT_MAIN.PRODUCTCODE') + ': ' + row.productCode
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                hidden: !_this.param.step || _this.param.step !== 'edit',
                onClick: function () {
                    var grid = Gikam.getComp('lims-outsourcers-product-main-edit-list-grid')
                    Gikam.create('modal', {
                        width: '80%',
                        height: '80%',
                        title: Gikam.choose.getConfig('product-choose').title,
                        url: IFM_CONTEXT + Gikam.choose.getConfig('product-choose').url + Gikam.param({
                            status_EQ: '1',
                            // single: true,
                            target_filter: encodeURI(JSON.stringify({
                                type: 'unassigned',
                                targetTable: 'T_LIMS_OUTSOURCERS_PRODUCT_MAIN',
                                targetMatchColumn: 'productMainId',
                                thisMatchColumn: 'id',
                                filter: [{
                                    targetFilterColumn: 'outsourcerId',
                                    targetFilterValue: _this.param.id
                                }]
                            }))
                        }),
                        onAfterClose: function (rows) {
                            if (!Gikam.isEmpty(rows)) {
                                var data = rows.map(r => {
                                    return {
                                        productMainId: r.id,
                                        productCode: r.productCode,
                                        productName: r.productName,
                                        productType: r.productType,
                                        standardCycle: r.standardCycle,
                                        outsourcerId: _this.param.id
                                    }
                                })
                                workspace.window.showMask()
                                Gikam.postText(limsOutsourcers.baseProductMainUrl + '/insert-multi', Gikam.getJsonWrapper(null, [null, data])).done(req => {
                                    grid.refresh()
                                }).always(function () {
                                    workspace.window.closeMask()
                                    Gikam.getLastModal().close()
                                })
                            }
                        }
                    })
                    /*                    Gikam.preInsert({
                                            modalTitle: 'BASEMODULE.BUSINESS.OUTSOURCERSS.MODAL.ADD',
                                            fields: limsOutsourcers.getProductMainBaseInfoFormFields(),
                                            url: limsOutsourcers.baseProductMainUrl,
                                            param: {
                                                outsourcerId: _this.param.id
                                            }
                                        }).done(function (id) {
                                            Gikam.getComp('lims-outsourcers-product-main-edit-list-grid').refresh()
                                        })*/
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                hidden: !_this.param.step || _this.param.step !== 'edit',
                onClick: function () {
                    Gikam.getComp('lims-outsourcers-product-main-edit-list-grid').deleteRows(limsOutsourcers.baseProductMainUrl)
                }
            }]
        }
    },

    getBaseInfoTab: function () {
        var _this = this
        return {
            type: 'tab',
            panels: [{
                title: 'BASEMODULE.BUSINESS.OUTSOURCERSS.TAB.BASE_INFO',
                items: [{
                    type: 'form',
                    url: _this.getCompUrl('baseInfoForm'),
                    service: 'limsOutsourcersServiceImpl',
                    dbTable: 'T_LIMS_OUTSOURCERS',
                    id: 'lims-outsourcers-detail-base-info-form',
                    fields: limsOutsourcers.getBaseInfoFormFields(),
                    readonly: !_this.param.step || _this.param.step !== 'edit',
                    onRendered: function () {
                        this.toFieldsReadonly(['outsourcerCode'])
                    }
                }]
            }, {
                title: 'T_LIMS_OUTSOURCERS_PRODUCT_MAIN.SERVICE_NAME',
                items: [_this.getOutsourcerProductMainGrid()]
            }, {
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    id: 'lims-outsourcers-detail-uploader',
                    dbTable: 'T_LIMS_OUTSOURCERS',
                    bizId: _this.param.id,
                    readonly: !_this.param.step || _this.param.step !== 'edit'
                }]
            }]
        }
    },

    urlManager: {
        baseInfoForm: limsOutsourcers.baseUrl + '/{id}',
    },

    getCompUrl: function (name) {
        if (Gikam.isEmpty(this.param.id)) {
            return ''
        }
        return Gikam.printf(this.urlManager[name], {
            id: this.param.id
        })
    },

    createPage: function () {
        Gikam.create('layout', {
            center: {
                items: [this.getBtnToolbar(), this.getBaseInfoTab()]
            },
            renderTo: workspace.window.$dom
        })
        return this
    },

    init: function (param) {
        this.param = param
        this.createPage()
    }
}
